const fs = require('fs');
const path = require('path');
const iconv = require('iconv-lite');

function debugCDATAMatching() {
  const testFile = path.join(__dirname, '..', 'resources', 'dic', 'a', 'abandon.txt');
  
  console.log(`测试文件: ${testFile}`);
  
  try {
    // 使用iconv-lite读取GBK编码的文件
    const buffer = fs.readFileSync(testFile);
    const content = iconv.decode(buffer, 'gbk');
    
    console.log('文件中包含词典音标的部分:');
    const startTagIndex = content.indexOf('<词典音标>');
    const endTagIndex = content.indexOf('</词典音标>');
    
    if (startTagIndex !== -1 && endTagIndex !== -1) {
      const betweenTags = content.substring(startTagIndex, endTagIndex + '</词典音标>'.length);
      console.log(betweenTags);
      
      console.log('\n词典音标标签之间的内容:');
      const innerContent = content.substring(startTagIndex + '<词典音标>'.length, endTagIndex);
      console.log('"' + innerContent + '"');
      
      console.log('\n尝试匹配CDATA:');
      const cdataMatch1 = innerContent.match(/<!$$CDATA$$([^\]]+)$$]]>/);
      console.log('匹配结果1:', cdataMatch1);
      
      const cdataMatch2 = innerContent.match(/<!$$CDATA$$([^]]+)$$]]>/);
      console.log('匹配结果2:', cdataMatch2);
      
      const cdataMatch3 = innerContent.match(/<!$$CDATA$$([^]]*)$$]]>/);
      console.log('匹配结果3:', cdataMatch3);
    } else {
      console.log('未找到词典音标标签');
    }
    
  } catch (error) {
    console.error(`处理文件时出错:`, error.message);
  }
}

debugCDATAMatching();